using System;
using System.IO;
using EasyDapr.Infrastructure;
using EasyDapr.Infrastructure.Dapr;
using EasyDapr.Infrastructure.EfCore;
using EasyDapr.Infrastructure.Validator;
using InventoryService.Infrastructure.Data;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace InventoryService.Test
{
    public class TestBase
    {
        /// <summary>
        /// 服务提供程序，用于获取服务实例
        /// </summary>
        protected readonly IServiceProvider Provider;

        public TestBase()
        {
            var services = new ServiceCollection();
            var configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", false)
                .Build();
            
            services.AddHttpContextAccessor()
                .AddCustomMediatR<Anchor>()
                .AddCustomValidators<Anchor>()
                .AddCustomDbContext<MainDbContext, Anchor>(configuration.GetConnectionString("mysql"))
                .AddCustomDaprClient()
                .AddControllers()
                .AddDapr();
            
            services.AddLogging(loggingBuilder =>
            {
                loggingBuilder.AddConfiguration(configuration.GetSection("Logging"));
                loggingBuilder.AddConsole();
                loggingBuilder.AddDebug();
            });
            
            Provider = services.BuildServiceProvider();
        }
    }
}